{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1=1\t\n",
      "1x2=2\t2x2=4\t\n",
      "1x3=3\t2x3=6\t3x3=9\t\n",
      "1x4=4\t2x4=8\t3x4=12\t4x4=16\t\n",
      "1x5=5\t2x5=10\t3x5=15\t4x5=20\t5x5=25\t\n",
      "1x6=6\t2x6=12\t3x6=18\t4x6=24\t5x6=30\t6x6=36\t\n",
      "1x7=7\t2x7=14\t3x7=21\t4x7=28\t5x7=35\t6x7=42\t7x7=49\t\n",
      "1x8=8\t2x8=16\t3x8=24\t4x8=32\t5x8=40\t6x8=48\t7x8=56\t8x8=64\t\n",
      "1x9=9\t2x9=18\t3x9=27\t4x9=36\t5x9=45\t6x9=54\t7x9=63\t8x9=72\t9x9=81\t\n"
     ]
    }
   ],
   "source": [
    "for a in range(1, 10):\n",
    "    for b in range(1, a+1):\n",
    "        print('{}x{}={}\\t'.format(b, a, a*b), end='')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:asdfgfdsa\n",
      "是\n"
     ]
    }
   ],
   "source": [
    "a = input('请输入一个字符串:')\n",
    "b = reversed(list(a))\n",
    "if list(b)==list(a):\n",
    "    print('是')\n",
    "else:\n",
    "    print('否')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的分数96\n",
      "优秀\n"
     ]
    }
   ],
   "source": [
    "score = int(input('请输入你的分数'))\n",
    "if score >= 90:\n",
    "    print('优秀')\n",
    "elif 60 <= score < 90:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "#coding=gbk\n",
    "#推荐使用第一种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-34-9311e5af60af>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-34-9311e5af60af>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    \"\"计算并返回a到b范围内数据的平均值\"\"\"\"\u001b[0m\n\u001b[0m                      ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 不要在文档注释中复制函数原型，而是要描述具体内容，解释具体参数和返回值等\n",
    "def function(a,b):\n",
    "    \"\"计算并返回a到b范围内数据的平均值\"\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对函数的参数、返回值等的说明采用NumPy标准\n",
    "def func(arg1,arg2):\n",
    "    \"\"在这里写函数的一句话总结（如：计算平均值）\n",
    "    这里是具体描述\n",
    "    参数\n",
    "    ----------\n",
    "    arg1 : int\n",
    "        arg1的具体描述\n",
    "    arg2 : int\n",
    "        arg2的具体描述\n",
    "    返回值\n",
    "    -------\n",
    "    int\n",
    "         返回值的具体描述\n",
    "    参看\n",
    "    --------\n",
    "    otherfunc: 其他关联函数等\n",
    "    示例\n",
    "    --------\n",
    "    示例使用doctest格式,,`>>>`后面的代码可以被文档测试工具作为测试用例自动运行\n",
    "    >>>a[1,2,3]\n",
    "    >>>print[x+3 for x in a]\n",
    "    [4,5,6]\n",
    "    \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#模块级函数和类定义之间的空两行，类成员函数之间空一行。\n",
    "class A:\n",
    "    \n",
    "    \n",
    "    def_init_(self):\n",
    "        pass\n",
    "    \n",
    "    \n",
    "    def hello(self):\n",
    "        pass\n",
    "    \n",
    "    \n",
    "def main():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将第二行缩进到括号的起始处。\n",
    "foo = long_function_name(var_one,var_two,\n",
    "                         var_three,var_four)\n",
    "#将第二行缩进4个空格，适用于从起始括号就换行的情形。\n",
    "def long_function_name(\n",
    "    var_one,var_two,var_three,\n",
    "    var_four):\n",
    "    print（var_one）\n",
    "#使用反斜杠\\换行，二元运算符+等应出现在行末；长字符串也可以用此法换行。\n",
    "session.query(MyTable).\\\n",
    "          filter_by(id=1).\\\n",
    "          one()\n",
    "\n",
    "\n",
    "print'Hello,'\\\n",
    "       '%s%s!%\\\n",
    "       ('Harry','Potter')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#正确的模块名\n",
    "import decoder\n",
    "import html_parser\n",
    "#类名\n",
    "class Farm():\n",
    "    pass\n",
    "\n",
    "\n",
    "class AnimalFarm(Farm):\n",
    "    pass\n",
    "\n",
    "\n",
    "class _PrivateFarm(Farm):\n",
    "    pass\n",
    "#函数名\n",
    "def run():\n",
    "    pass\n",
    "\n",
    "\n",
    "def run_with_env():\n",
    "    pass\n",
    "\n",
    "\n",
    "class Person():\n",
    "    \n",
    "    \n",
    "    def_private_func():\n",
    "        pass\n",
    "#变量名\n",
    "if_name_=='_main_':\n",
    "    count = 0\n",
    "    school_name=\"\n",
    "#常量名\n",
    "MAX_CLIENT = 100\n",
    "MAX_CONNECTION = 1000\n",
    "CONNECTION_TIMEOUT = 600\n",
    "#保留字\n",
    "import keyword\n",
    "keyword.kwlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter = 100       #整型变量\n",
    "miles = 1000.0      #浮点型变量\n",
    "name = \"runoob\"     #字符串\n",
    "#同时为多个变量赋值\n",
    "a = b = c = 1\n",
    "a,b,c = 1,2,\"runoob\"\n",
    "#数据类型  Number(数字)、String(字符串)、List(列表)、Tuple(元组)、Set(集合)、Dictionary(字典)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数字类型\n",
    "#int(整型)\n",
    "#long(长整型)\n",
    "#flot(浮点型)\n",
    "#complex(复数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5+4     #加法\n",
    "4.3-2   #减法\n",
    "3*7     #乘法\n",
    "2/4     #除法，得到一个浮点数\n",
    "2//4    #除法，得到一个整数\n",
    "17%3    #取余\n",
    "2**5    #乘方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hellowworld\n",
      "hellowworl\n",
      "h\n",
      "llo\n",
      "llowworld\n",
      "hellowworldhellowworld\n",
      "hellowworldTEST\n"
     ]
    }
   ],
   "source": [
    "#字符串截取\n",
    "#变量[头下标:尾下标]\n",
    "#从后面索引:      -6  -5  -4  -3  -2  -1\n",
    "#从前面索引:       0   1   2   3   4   5 \n",
    " #             +---+---+---+---+---+---+\n",
    " #             | a | b | c | d | e | f |\n",
    " #             +---+---+---+---+---+---+\n",
    "#从前面截取:     :   1   2   3   4   5   :\n",
    "#从后面截取:     :  -5  -4  -3  -2  -1   :\n",
    "            \n",
    "str = 'hellowworld'\n",
    "\n",
    "print(str)        #输出字符串\n",
    "print(str[0:-1])  #输出第一个到倒数第二个的所有字符\n",
    "print(str[0])     #输出字符串的第一个字符\n",
    "print(str[2:5])   #输出从第三个开始到第五个的所有字符\n",
    "print(str[2:])    #输出从第三个开始的所有字符\n",
    "print(str*2)      #输出字符串两次\n",
    "print(str+\"TEST\") #连接字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P n\n",
      "n P\n"
     ]
    }
   ],
   "source": [
    "word = 'Python'\n",
    "print(word[0],word[5])\n",
    "print(word[-1],word[-6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# !=  比较两个对象是否不相等\n",
    "a = 2\n",
    "b = 10\n",
    "a != b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  +=  \n",
    "  c += a  等效于  c=c+a\n",
    "#  -=\n",
    "  c -= a  等效于  c=c-a\n",
    "#  *=\n",
    "  c *= a  等效于  c=c*a\n",
    "#  /=  \n",
    "  c /= a  等效于  c=c/a\n",
    "#  %=\n",
    "#  **=\n",
    "#  //="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=6\n",
    "b=7\n",
    "a&b\n",
    "#0110&0111=0110(6)\n",
    "#两个为1则为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1\n",
    "b=2\n",
    "a|b\n",
    "#0001|0010=0011(3)\n",
    "#有一个1则为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1\n",
    "b=3\n",
    "a^b\n",
    "#0001^0011=0010(2)\n",
    "#两个相异则为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1\n",
    "~a\n",
    "#1变0,0变1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=2\n",
    "a<<1\n",
    "#0010→0100(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=8\n",
    "a>>1\n",
    "#1000→0100(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='a'\n",
    "b='cba'\n",
    "a in b\n",
    "#True\n",
    "\n",
    "b=list(b)\n",
    "b\n",
    "#['c','b','a']\n",
    "\n",
    "a in b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='a'\n",
    "b='cba'\n",
    "a not in b\n",
    "#False\n",
    "\n",
    "b=list(b)\n",
    "b\n",
    "['c','b','a']\n",
    "\n",
    "a not in b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-89-8d784ff87248>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
     ]
    }
   ],
   "source": [
    "#身份运算符\n",
    "x is y  #引用对象\n",
    "类似于\n",
    "id(x)==id(y)  #变量值\n",
    "\n",
    "x is not y\n",
    "类似于\n",
    "id(x) != id(y)\n",
    "#id()函数用于获取对象的内存地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数据fgjlkff\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'fgjlkff'"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=input('请输入数据')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入第一个数字5\n",
      "输入第二个数字6\n",
      "数字5和6相加结果为:    11.0\n"
     ]
    }
   ],
   "source": [
    "#求和#\n",
    "num1=input('输入第一个数字')\n",
    "num2=input('输入第二个数字')\n",
    "sum=float(num1)+float(num2)\n",
    "print('数字{0}和{1}相加结果为:    {2}'.format(num1,num2,sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-98-6e3131a45f52>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-98-6e3131a45f52>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    print('两数之和为%.1f%(float(input('输入第一个数字: '))+float(input('输入第二个数字: '))))\u001b[0m\n\u001b[0m                                         ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#_*_coding:UTF_8_*_\n",
    "#Filename : ch02sum.py\n",
    "print('两数之和为%.1f%(float(input('输入第一个数字: '))+float(input('输入第二个数字: '))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#水仙数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
